﻿/*
 * Created by SharpDevelop.
 * User: Intel
 * Date: 29.11.2014
 * Time: 12:33
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Region
{
  /// <summary>
  /// Description of OtdelSelectorForm.
  /// </summary>
  public partial class OtdelSelectorForm : Form
  {
    protected OtdelList m_otdelListNull;
    protected Engine m_engine;
    protected OtdelList m_otdelList; 
    protected OtdelList m_currentOtdelList; 
    protected Int32 m_OtdelID;
    public Engine engine{get{return m_engine;}set{m_engine=value;change();}}
    public OtdelList otdelList{get{return m_otdelList;}set{m_otdelList=value;change();}}
    public Int32 OtdelID{get{return m_OtdelID;}set{m_OtdelID=value;change();}}
    public delegate void EventHandlerOnSelectOtdel(Int32 OtdelID);    
    public event EventHandlerOnSelectOtdel OnSelectOtdel;    
    Dictionary<Int32,TreeNode> listNodes;
    protected void changeOtdelID(Int32 OtdelID)
    {
      if (m_OtdelID == OtdelID) return;
      m_OtdelID = OtdelID;
      if (listNodes!=null && listNodes.ContainsKey(m_OtdelID) && treeView1.SelectedNode != listNodes[m_OtdelID])
        treeView1.SelectedNode = listNodes[m_OtdelID];
      if (OnSelectOtdel!=null) 
        OnSelectOtdel(m_OtdelID);
    }
    protected void change()
    {
      if (m_engine==null) return;
    }    
    public OtdelSelectorForm()
    {
      //
      // The InitializeComponent() call is required for Windows Forms designer support.
      //
      InitializeComponent();
      
      //
      // TODO: Add constructor code after the InitializeComponent() call.
      //
    }
    public void ShowList()
    {
      if (m_engine==null) return;
      m_currentOtdelList = m_otdelList;
      if (m_currentOtdelList==null)
      {
        if (m_otdelListNull==null) m_otdelListNull = new OtdelList(m_engine);
        m_currentOtdelList = m_otdelListNull;
      }
      m_currentOtdelList.Filter();
      treeView1.Nodes.Clear();
      var Otdels = new Dictionary<Int32, Boolean>();
      var OtdelsSelect = new Dictionary<Int32, Boolean>();
      var OtdelsNode = new Dictionary<Int32, TreeNode>();
      listNodes = new Dictionary<Int32, TreeNode>();
      for(int i=0;i<m_currentOtdelList.NumLines;i++)
      {
        OtdelListLine line = m_currentOtdelList.getLine(i);
        OtdelsSelect[line.OtdelID] = true;
        if (Otdels.ContainsKey(line.OtdelID)) continue;
        Otdel otdel = m_engine.getOtdel(line.OtdelID);
        while(otdel!=null && !Otdels.ContainsKey(otdel.OtdelID))
        {
          Int32 tmpOtdelID = otdel.OtdelID;
          otdel = m_engine.getOtdel(otdel.ParentOtdelID);
          Otdels[tmpOtdelID] = otdel==null;
        }        
      }
      Boolean workOtdel = true;      
      while(workOtdel)
      {
        workOtdel = false;
        foreach (var o in Otdels)
          if (!OtdelsNode.ContainsKey(o.Key))
        {
          Otdel otdel = m_engine.getOtdel(o.Key);
          TreeNode node = null;
          if (OtdelsNode.ContainsKey(otdel.ParentOtdelID))
          {
            node = new TreeNode(otdel.OtdelName);            
            OtdelsNode[otdel.OtdelID] = node;
            OtdelsNode[otdel.ParentOtdelID].Nodes.Add(node);
            workOtdel = true;
          }
          else if (o.Value)
          {
            node = new TreeNode(otdel.OtdelName);            
            OtdelsNode[otdel.OtdelID] = node;
            treeView1.Nodes.Add(node);
            workOtdel = true;
          }
          if (node!=null)
          {
            if (OtdelsSelect.ContainsKey(otdel.OtdelID))
            {    
              node.Tag = otdel.OtdelID;
              listNodes[otdel.OtdelID] = node;
            }
            else
            {
              node.NodeFont = new Font(this.Font,FontStyle.Bold);
            }
          }
        }        
      }
      foreach(var os in OtdelsSelect)
        if (OtdelsNode.ContainsKey(os.Key))
      {
        
      }
      Show();
    }
    void OtdelSelectorFormDeactivate(object sender, EventArgs e)
    {
      Hide();
    }
    void TreeView1BeforeSelect(object sender, TreeViewCancelEventArgs e)
    {
		  if (e.Node!=null && e.Node.Tag is Int32)
		  {
		    changeOtdelID((Int32)e.Node.Tag);
		  }  
    }
  }
}
